<?php

namespace app\apisell\controller\xcx;

use app\apisell\controller\admin\Exception;
use app\BaseController;
use app\common\utils\Res;
use think\facade\Db;

class UserPayApply extends BaseController
{

    /**
     * 提现申请
     */
    public function userPayApply()
    {
        $param = $this->request->param();
        $userId =  $this->request->id;//获取用户id
        Db::startTrans();
        try {
            $user = Db::table('xh_sell_user')->where(['id' => $userId])->find();
            if ($user) {
                //新增审批记录
                $data = [
                    'user_id' => $user['id'],
                    'type' => 1,
                    'status' => 1,
                    'apply_for_time' => date('Y-m-d H:i:s',time()),
                    'money' => $param['money'],
                ];
                Db::table('xh_sell_user_pay')->insert($data);
                //更改用户表
                $old_await = $user['money_pay_await'];
                $old_loading = $user['money_pay_loading'];
                $nwe_await = $old_await - $param['money'];
                $new_loading = $old_loading + $param['money'];
                Db::table('xh_sell_user')->where(['id' => $user['id']])->update(['money_pay_await' => $nwe_await, 'money_pay_loading' => $new_loading]);
                
                //添加审批记录
                $popuUser = Db::table('xh_sell_popu_user')->where(['popu_user_id' => $user['id']])->find();
                $logData = [
                    'user_pay_id' => $userId,
                    'type' => 1,
                    'desc' => '申请日期:'.date('Y-m-d',time()).'申请人:'.$popuUser['popu_name'].'申请提现【'.$param['money'].'】元。',
                    'change_json' => '[{"field":"money_pay_await","former":'.$old_await.',"after":'.$nwe_await.'},{"field":"money_pay_loading","former":'.$old_loading.',"after":'.$new_loading.'}]',
                    'change_time' => date('Y-m-d H:i:s',time()),
                ];
                Db::table('xh_sell_user_pay_log')->insert($logData);
            }else {
                return Res::error('未查到该推广员');
            }
            Db::commit();
            return Res::ok();
        }catch (Exception $e) {
            Db::rollback();
            // 记录错误日志
            error_log($e->getMessage());
            return Res::error('提现申请失败');
        }
       
    }

    public function userPayInfo()
    {
        $param = $this->request->param();
        $size = $param['pageSize'] ? $param['pageSize'] : 10;  //每页显示条数
        $page = $param['page'] ? $param['page'] : 1;  //当前页数
        $userId =  $this->request->id;//获取用户id
//        $userId =  $param['id'];//获取用户id
        $where = array();
        $where[] = array('p.user_id', '=', $userId);
        //查询提现审批
        $result = Db::table('xh_sell_user_pay')->alias('p')
            ->field('p.*, pu.popu_name,u.money_balance')
            ->leftJoin('xh_sell_user u','p.user_id = u.id')
            ->leftJoin('xh_sell_popu_user pu','u.id = pu.popu_user_id')
            ->where($where)
            ->page($page, $size)
            ->select()->toArray();

        foreach ($result as $key => $value) {
            //申请状态
            switch ($value['status']) {
                case 1: $result[$key]['stype'] = '审核中'; break;
                case 2: $result[$key]['stype'] = '审合通过'; break;
                case 3: $result[$key]['stype'] = '已拒绝'; break;
                case 4: $result[$key]['stype'] = '打款中'; break;
                case 5: $result[$key]['stype'] = '打款失败'; break;
                case 6: $result[$key]['stype'] = '已到账'; break;
            }

        }
        $count = Db::table('xh_sell_user_pay')->alias('p')
            ->field('p.*, pu.popu_name,u.money_balance')
            ->leftJoin('xh_sell_user u','p.user_id = u.id')
            ->leftJoin('xh_sell_popu_user pu','u.id = pu.popu_user_id')
            ->where($where)
            ->count();
        $data = [
            'list' => $result,
            'total' => $count
        ];
        return Res::ok($data);
    }
    
}